const path = require('path')
const fs = require('fs-extra')
const inquirer = require('inquirer')
const download = require('../lib/downloadRepo')

module.exports = async function (name, options) {
  // console.log(name)
  // 当前命令行选择的目录
  const cwd = process.cwd()
  // 需要创建的目录地址
  const target = path.join(cwd, name)
  // 目录是否已经存在？
  if (fs.existsSync(target)) {
    if (options.force) {
      await fs.remove(target)
    } else {
      //TODO: 询问用户是否确定要覆盖
      let { action } = await inquirer.prompt([
        {
          name: 'action',
          type: 'list',
          message: 'Target directory already exists Pick an action:',
          choices: [
            {
              name: 'Overwrite',
              value: 'overwrite'
            },{
              name: 'Cancel',
              value: false
            }
          ]
        }
      ])
      if (action) {
        await fs.remove(target)
      } else {
        return
      }
    }
  }
  //TODO: 选择模板
  let { action } = await inquirer.prompt([
    {
      name: 'action',
      type: 'list',
      message: 'please choose a template to create project',
      choices: [
        {
          name: 'vite-vue-ts-h5',
          value: 'h5'
        },
        {
          name: 'vite-vue-ts-pc',
          value: 'pc'
        },
        {
          name: 'nestjs-be',
          value: 'nestjs'
        }
      ]
    }
  ])
  if (action) {
    await download(action, name, {clone: true})
  }
}